﻿//////////////////////////////////////////////////////////////////////////
// FileName:fmManagePolicyManager.aspx
// CreateBy:DragonXiong
// CreateDate:2010-05-25
// Description:
//		  Manage Policy Manager
//      
// Modify Log:
//	
// Copyright:
/////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Auto.Web.Common;
using Auto.Components;
using Model = Auto.Data.BusMan.Model;
using BLL = Auto.Data.BusMan.BLL;
using FreeTextBoxControls;
namespace Auto.BusMan
{
    public partial class fmManagePolicyManager : System.Web.UI.Page
    {
        public string DocID
        {
            get
            {
                ViewState["DocID"] = Request.QueryString["DocID"];
                return (ViewState["DocID"] == null) ? "" : ViewState["DocID"].ToString();
            }
            set { ViewState["DocID"] = value; }
        }

        Model.ManagementPolicy model = new Model.ManagementPolicy();
        BLL.ManagementPolicy bll = new BLL.ManagementPolicy();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
            }
        }

        private void InitPage()
        {
            txtMainContent.AutoConfigure = AutoConfigure.EnableAll;
            this.txtMainContent.HelperFilesPath = "HelperScripts";
            this.txtMainContent.ImageGalleryPath = "BusMan/UpFile/Policy";
            //string UpNewsImageFolder = ConfigHelper.GetConfigString("UpNewsImageFolder");
            //this.txtMainContent.ImageGalleryPath = UpNewsImageFolder;

            if (string.IsNullOrEmpty(DocID)) //新增
            {
                this.chkAddContinue.Checked = true;
                checktr.Visible = true;
                checktr.Attributes.Add("style", "display:block;");
            }
            else  //修改
            {
                checktr.Attributes.Add("style", "display:none;");
                InitControl();
            }
        }

        private void ClearText()
        {
            txtFileName.Text = "";
            txtReleaseDivID.Text = "";
            txtReleaseDate.Text = "";
            ucPolicyFileFileManager.FileList = "";
            
            txtMainContent.Text = "";
        }

        private void InitControl()
        {

            model = bll.GetModel(DocID);
            Session["fmManagePolicyManager.Model"] = model;
            txtFileName.Text = model.FileName;
            txtReleaseDate.CalendarDate = model.ReleaseDate;
            txtReleaseDivID.Text = model.ReleaseDivID;
            ucPolicyFileFileManager.FileList = model.PolicyFile;
            txtMainContent.Text = model.MainContent;// HtmlDiscode(model.MainContent);
        }

        private Model.ManagementPolicy GetManagementPolicy()
        {
            if (!string.IsNullOrEmpty(DocID))
            {
                model = (Model.ManagementPolicy)Session["fmManagePolicyManager.Model"];
            }
            //Tab1
            string FileName = this.txtFileName.Text;
            DateTime ReleaseDate =StrHelper.GetDateTimeValue(this.txtReleaseDate.CalendarDate);
            string ReleaseDivID = this.txtReleaseDivID.Text;
            string PolicyFile = this.ucPolicyFileFileManager.FileList;
            string MainContent = this.txtMainContent.Text;
            //string Note = this.txtNote.Text;

            //Tab1
            model.FileName = FileName;
            model.ReleaseDate = ReleaseDate;
            model.ReleaseDivID = ReleaseDivID;
            model.PolicyFile = PolicyFile;
            model.MainContent = MainContent;// HtmlEncode( MainContent);
           
            //model.Note = Note;

            return model;


        }

        #region Event

        protected void btnSave_Click(object sender, EventArgs e)
        {

            model = GetManagementPolicy();

            model.UserAcct = SessionBox.User.UserID;
            try
            {
                if (string.IsNullOrEmpty(DocID))
                {
                    model.RecDivID = SessionBox.User.DivID;
                    bll.Add(model);
                }
                else
                {
                    bll.Update(model);
                }
                Redirect();

            }
            catch (Exception ex)
            {
                Jscript.AjaxAlert(this, ex.Message);
            }
        }

        private void Redirect()
        {
            if (chkAddContinue.Checked)
            {
                lblMsg.Text = "保存成功，请继续添加！";
                ClearText();
            }
            else
            {
                Server.Transfer("fmManagePolicyList.aspx");
            }
        }

        #endregion Event

        /// <summary>
        /// 替换html中的特殊字符
        /// </summary>
        /// <param name="theString">需要进行替换的文本。</param>
        /// <returns>替换完的文本。</returns>
        private string HtmlEncode1(string theString)
        {
            theString = theString.Replace(">", "&gt;");
            theString = theString.Replace("<", "&lt;");
            theString = theString.Replace("  ", " &nbsp;");
            theString = theString.Replace("  ", " &nbsp;");
            theString = theString.Replace("\"", "&quot;");
            theString = theString.Replace("\'", "&#39;");
            theString = theString.Replace("\n", "<br/> ");
            return theString;
        }

        /// <summary>
        /// 恢复html中的特殊字符
        /// </summary>
        /// <param name="theString">需要恢复的文本。</param>
        /// <returns>恢复好的文本。</returns>
        private  string HtmlDiscode1(string theString)
        {
            theString = theString.Replace("&gt;", ">");
            theString = theString.Replace("&lt;", "<");
            theString = theString.Replace("&nbsp;", " ");
            theString = theString.Replace(" &nbsp;", "  ");
            theString = theString.Replace("&quot;", "\"");
            theString = theString.Replace("&#39;", "\'");
            theString = theString.Replace("<br/> ", "\n");
            return theString;
        }
    }
}